<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="700"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <sk-tabs type="border-card">
      <el-tab-pane ref="tab" label="信息">
        <el-form
          :model="form"
          :rules="rules"
          ref="dialogForm"
          label-width="100px"
          label-position="right"
        >
          <el-row>
            <el-col :span="22">
              <el-form-item label="编号" prop="name">
                <el-input v-model="form.code" disabled placeholder="自动生成"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="名称" prop="name">
                <el-input v-model="form.name" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="客户" prop="company_id">
                <skAutoChoose
                  v-model="form.company.name"
                  :name="'customer'"
                  :assign="form"
                  :asKey="'company_id'"
                >
                </skAutoChoose>
              </el-form-item>
            </el-col>
            <el-col :span="22">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-tab-pane>
    </sk-tabs>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
import skAutoChoose from "@/components/skAutoChoose";
export default {
  emits: ["success", "closed", "update:modeValue"],
  components: { skAutoChoose },
  data() {
    return {
      mode: "add",
      diccode: "CostType",
      unit: "All",
      titleMap: {
        add: "新增",
        edit: "编辑",
      },
      form: {
        id: "",
        name: "",
        company: {},
        remark: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名称" }],
      },

      visible: false,
      isSaveing: false,
    };
  },
  mounted() {},
  methods: {
    //显示
    close() {
      this.visible = false;
      this.$emit("update:modeValue", false);
      this.$emit("closed");
    },
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },

    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = await this.$API.system.barcode.save.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit("success", this.form, this.mode);
            this.visible = false;
            this.$message.success("操作成功");
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      this.form = {
        id: "",
        name: "",
        company: {},
        remark: "",
      };
      Object.assign(this.form, this.$TOOL.removeNull(data));
    },
  },
};
</script>

<style></style>
